/**
 * 注销
 */
function logout() {
    $.ajax({
        "url": "login/logout.action",
        "method": "POST",
        "dataType": "json",
        "success": function (data) {
            if (data['status'] === 1) {
                window.location.href = "login.html";
                return;
            }
            alert(data['data'])
        },
        "error": function () {
            alert("注销失败")
        }
    })
}

/**
 * 显示拿到的学期数据
 * @param termData 学期数据
 * @param select 显示的下拉列表
 * @param after 回调函数
 */
function showTerms(termData, select, after) {
    select.html("");
    for (let i = 0; i < termData.length; i++) {
        let term = termData[i];
        let option = $("<option></option>");
        option.html(term['termName']);
        option.val(term['termId']);
        select.append(option);
    }
    if (after !== undefined) {
        after();
    }
}


/**
 * 显示拿到的成绩数据
 * @param termGradeData 成绩数据
 * @param show 显示的div
 * @param after 回调函数
 */
function showGrade(termGradeData, show, after) {
    show.html("");
    let size = 0;
    show.append("<div class='text-right mt-2'><button class='btn btn-primary' onclick='ext()'>导出</button></div>");
    show.parent().append(createModal());
    for (let i = 0; i < termGradeData.length; i++) {
        let temp = termGradeData[i];
        let termName = temp['termName'];
        let grades = temp['grades'];
        show.append($("<div class='mb-3 lead'>" + termName + "</div>"));
        for (let j = 0; j < grades.length; j++) {
            size++;
            let showDiv = $("<div class='mb-3'></div>");
            let showTable = $("<table id='showTable' class='table table-bordered'></table>");
            show.append(showDiv);
            showDiv.append(showTable);
            let grade = grades[j];
            showTable.append(getColspanTrh(grade['courseName']));
            showTable.append(getTrd("课程号", grade['courseNumber']));
            showTable.append(getTrd("成绩", grade['score'] === '-1' ? "暂未录入" : grade['score']));
            let score = parseFloat(grade['score']);
            if (score < 60 && score >= 0) {
                showTable.append(getTrd("未通过原因", grade['failReason']));
            }
            showTable.append(getColspanTrh("分项成绩"));
            let details = grade['details'];
            if (details.length === 0) {
                showTable.append(getColspanTrd("暂无信息"));
                continue;
            }
            showTable.append(getTrd3("分项名称", "分项成绩", "分项所占比重"));
            for (let k = 0; k < details.length; k++) {
                let detail = details[k];
                let weight = parseFloat(detail['weight']) * 100 + "%";
                showTable.append(getTrd3(detail['name'], detail['score'], weight));
            }
        }
        show.append("<div class='mt-3'></div>");
    }
    if (size === 0) {
        alert("该条件下未查询到成绩");
        show.html("该条件下未查询到成绩");
    }
    if (after !== undefined) {
        after();
    }
}

/**
 * 创建一个跨3列的表头
 * @param value 表头名
 * @returns {jQuery|HTMLElement} 创建好的表头
 */
function getColspanTrh(value) {
    return $("<tr><th colspan='3' scope='col'>" + value + "</th></tr>")
}

/**
 * 创建一个跨3列的普通表
 * @param value 显示的内容
 * @returns {jQuery|HTMLElement} 创建好的一行
 */
function getColspanTrd(value) {
    return $("<tr><td colspan='3'>" + value + "</td></tr>")
}

/**
 * 创建一个左1右2的一行
 * @param key 左值
 * @param value 右值
 * @returns {jQuery|HTMLElement} 创建好的一行
 */
function getTrd(key, value) {
    return $("<tr><td>" + key + "</td><td colspan='2'>" + value + "</td></tr>")
}

/**
 * 创建一个1 1 1 的一行
 * @param v1 值1
 * @param v2 值2
 * @param v3 值3
 * @returns {jQuery|HTMLElement}
 */
function getTrd3(v1, v2, v3) {
    return $("<tr><td>" + v1 + "</td><td>" + v2 + "</td><td>" + v3 + "</td></tr>")
}

/**
 * button的禁用与开启
 * @param button 按钮
 * @param flag 禁用-true 启用-false
 * @param text 操作后按钮上的文字[可选]
 */
function disableButton(button, flag, text) {
    if (text === undefined) {
        text = button.html();
    }
    if (flag) {
        button.attr("disabled", "disabled");
        button.html(text);
    } else {
        button.removeAttr("disabled");
        button.html(text);
    }
}


function exportCol(button) {
    disableButton(button, true, "导出中");
    disableButton(button.prev(), true, "导出中");
    ex("0", button.prev(), button);
}

function exportRow(button) {
    disableButton(button, true, "导出中");
    disableButton(button.next(), true, "导出中");
    ex("1", button, button.next());
}

function ex(type, button1, button2) {
    $.ajax({
        "url": "grade/export.action",
        "method": "POST",
        "data": {
            "type": type
        },
        "dataType": "json",
        "success": function (data) {
            disableButton(button1, false, "按行导出");
            disableButton(button2, false, "按列导出");
            if (data['status'] === 1) {
                window.location.href = "xlsx/" + data['data'];
                $("#exDialog").modal("hide");
                return;
            }
            alert(data['data']);
        },
        "error": function () {
            disableButton(button1, false, "按行导出");
            disableButton(button2, false, "按列导出");
            alert("失败");
        }
    })
}

function createModal() {
    return $("<div class=\"modal fade\" id=\"exDialog\" data-backdrop=\"static\" tabindex=\"-1\" role=\"dialog\"\n" +
        "     aria-labelledby=\"staticBackdropLabel\" aria-hidden=\"true\">\n" +
        "    <div class=\"modal-dialog\" role=\"document\">\n" +
        "        <div class=\"modal-content\">\n" +
        "            <div class=\"modal-header\">\n" +
        "                <h5 class=\"modal-title\">提示</h5>\n" +
        "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n" +
        "                    <span aria-hidden=\"true\">&times;</span>\n" +
        "                </button>\n" +
        "            </div>\n" +
        "            <div class=\"modal-body\">\n" +
        "                请选择导出方式\n" +
        "            </div>\n" +
        "            <div class=\"modal-footer\">\n" +
        "                <button type=\"button\" class=\"btn btn-primary\" onclick=\"exportRow($(this))\">按行导出</button>\n" +
        "                <button type=\"button\" class=\"btn btn-primary\" onclick=\"exportCol($(this))\">按列导出</button>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>");
}

function ext() {
    $("#exDialog").modal("show");
}
